﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Beaker.FileSystem;

namespace OpenCube.IO.Overload
{
	internal sealed class OverloadingFile
		: OverloadingEntry<IFile>, IFile
	{
		internal OverloadingFile(OverloadingFolder parent, IEnumerable<IFile> files)
			: base(parent, files)
		{
			Contract.Requires<ArgumentNullException>(files != null, "At least one file is required.");
			Contract.Requires(files.Any(), "At least one file is required.");
		}

		internal IFolder GetActualParentFolder()
		{
			return ActiveEntry.ParentFolder;
		}

		/// <summary>
		/// Open the file for reading
		/// </summary>
		/// <returns>A stream of the content of the file</returns>
		public Stream OpenRead()
		{
			return ActiveEntry.OpenRead();
		}

		public Task<Stream> OpenReadAsync(CancellationToken cancellationToken)
		{
			return ActiveEntry.OpenReadAsync(cancellationToken);
		}

		/// <summary>
		/// Open the text-file for reading
		/// </summary>
		/// <returns>A text-reader with the content of the file</returns>
		public TextReader OpenText()
		{
			return ActiveEntry.OpenText();
		}

		public Task<TextReader> OpenTextAsync(CancellationToken cancellationToken)
		{
			return ActiveEntry.OpenTextAsync(cancellationToken);
		}

		public Stream OpenWrite()
		{
			throw new NotSupportedException();
		}

		public Task<Stream> OpenWriteAsync(CancellationToken cancellationToken)
		{
			throw new NotSupportedException();
		}
	}
}